/*
## 2023/11/02 # 铃 # 重写设定AI和玩家的初始资源,避免玩家前期发展期太长 ##

*/

namespace 设定资源
{

    class Main
    {

        pk::building @building_;
        pk::force @force_;

        Main()
        {
            pk::bind2(102, pk::trigger102_t(onGameStart), -2);
        }

        void onGameStart()
        {
            if (pk::get_scenario().loaded)
                return;

            if (pk::get_scenario().no == 血色剧本)
                return;

            update();

            return;
        }

        // 开局时设定基础数据
        void update()
        {

            for (int base_id = 0; base_id < 城市_末; ++base_id)
            {

                pk::building @building = pk::get_building(base_id);
                bool 大都市 = pk::is_large_city(pk::building_to_city(pk::get_building(base_id)));
                int level = pk::max(0, ch::get_base_p(base_id).city_level);
                // 开局设定兵役政策为2;
                auto building_p = ch::get_baseIA(building.get_id());
                building_p.building_policy = 2;

                float 难度修正 = 1.0;
                // 由于AI县城管理很紧张,早期AI缺钱会严重制约发展,因此AI的资金要多给
                if (!building.is_player())
                {
                    if (pk::get_scenario().difficulty == 难易度_超级)
                        难度修正 = 2.0;
                    if (pk::get_scenario().difficulty == 难易度_上级)
                        难度修正 = 1.5;
                    if (pk::get_scenario().difficulty == 难易度_初级)
                        难度修正 = 1.5;
                }

                 //pk::trace(pk::decode(pk::get_name(building)) + "  id:" + base_id);
                int 人数 = pk::get_person_list(building, pk::mibun_flags(身份_君主, 身份_都督, 身份_太守, 身份_一般)).count;

                int base_num = (5 + (大都市 ? 1 : 0) + level + pk::min(0, (人数 - 8) / 2));

                base_num = int(pk::max(base_num, 3) * 难度修正);
                base_num = pk::min(base_num, 10);

                if (pk::get_scenario().no == 7 and !building.is_player() and (base_id == 铃据点_云中 or base_id == 铃据点_朔方))
                    base_num = 20;


                if ((building.get_force_id()) == -1)
                {
                    pk::set_gold(building, 0);
                    pk::set_food(building, 0);
                    pk::set_troops(building, 0);


                    for (int weapon_id = 兵器_枪; weapon_id <= 兵器_战马; weapon_id++)
                    {
                        pk::set_weapon_amount(building, weapon_id, 0);
                    }

                    for (int weapon_id = 兵器_冲车; weapon_id <= 兵器_斗舰; weapon_id++)
                    {
                        pk::set_weapon_amount(building, weapon_id, 0);
                    }
                }


                if (building.get_force_id() != -1 and pk::get_taishu_id(building) != -1)
                {
                    pk::set_gold(building, int(base_num * 1000 * 难度修正));
                    pk::set_food(building, int(base_num * 10000 * 难度修正));
                    pk::set_troops(building, int(base_num * 5000));

                    for (int weapon_id = 兵器_枪; weapon_id <= 兵器_战马; weapon_id++)
                    {
                        if (pk::building_to_city(pk::get_building(base_id)).tokusan[pk::equipment_id_to_heishu(weapon_id)])
                            pk::set_weapon_amount(building, weapon_id, int(2000 * base_num * 难度修正));
                        else
                            pk::set_weapon_amount(building, weapon_id, int(1000 * base_num * 难度修正));
                    }

                    for (int weapon_id = 兵器_冲车; weapon_id <= 兵器_斗舰; weapon_id++)
                    {
                        if (pk::building_to_city(pk::get_building(base_id)).tokusan[pk::equipment_id_to_heishu(weapon_id)])
                            pk::set_weapon_amount(building, weapon_id, pk::max(0, int(3 + (难度修正 - 1) * 2 + (building.is_player() ? 0 : 2))));
                        else
                            pk::set_weapon_amount(building, weapon_id, pk::max(0, int(0 + (难度修正 - 1) * 2 + (building.is_player() ? 0 : 2))));
                    }
                }
            }

            for (int base_id = 城市_末; base_id < 据点_末; ++base_id)
            {
                int 难度修正 = 0;
                int 三顾刘备修正 = 0;

                pk::building @building = pk::get_building(base_id);
                string scenario_name = pk::decode(pk::get_scenario().name);
                if (pk::get_person_list(building, pk::mibun_flags(身份_君主)).count > 0)
                    三顾刘备修正 = 10000;

                if (!building.is_player())
                {
                    if (pk::get_scenario().difficulty == 难易度_超级)
                        难度修正 = 5000;
                    if (pk::get_scenario().difficulty == 难易度_上级)
                        难度修正 = 3000;
                    if (pk::get_scenario().difficulty == 难易度_初级)
                        难度修正 = 3000;
                }

                if ((building.get_force_id()) == -1)
                {
                    pk::set_gold(building, 0);
                    pk::set_food(building, 0);
                    pk::set_troops(building, 0);


                    for (int weapon_id = 兵器_枪; weapon_id <= 兵器_战马; weapon_id++)
                    {
                        pk::set_weapon_amount(building, weapon_id, 0);
                    }

                    for (int weapon_id = 兵器_冲车; weapon_id <= 兵器_斗舰; weapon_id++)
                    {
                        pk::set_weapon_amount(building, weapon_id, 0);
                    }
                }

                if ((building.get_force_id()) != -1)
                {
                    pk::set_gold(building, int(2000 + 难度修正 + 三顾刘备修正 / 10));
                    pk::set_food(building, int(20000 + 难度修正 * 10 + 三顾刘备修正));
                    pk::set_troops(building, int(2000 + 难度修正 + 三顾刘备修正));

                    for (int weapon_id = 兵器_枪; weapon_id <= 兵器_战马; weapon_id++)
                    {
                        pk::set_weapon_amount(building, weapon_id, int(1000.0 + 难度修正 / 2.0 + 三顾刘备修正 / 3.0 + (building.is_player() ? 0 : 1000)));
                    }

                    for (int weapon_id = 兵器_冲车; weapon_id <= 兵器_斗舰; weapon_id++)
                    {
                        pk::set_weapon_amount(building, weapon_id, int(pk::max(0.0, (难度修正 + 1000.0) / 2000.0 - 1.0 + (building.is_player() ? 0 : 1))));
                    }
                }
            }
        }
    } Main main;
}